//
//  systemViewController.m
//  LubanWork
//
//  Created by xiaohao on 2020/1/11.
//  Copyright © 2020 xiaohao. All rights reserved.
//

#import "systemViewController.h"
#import "RegisteredModel.h"
#import "messageTableViewCell.h"
@interface systemViewController ()<UITableViewDelegate,UITableViewDataSource>{
    MJRefreshAutoNormalFooter *footer;
}
@property(nonatomic,strong) UITableView * table;
@property(nonatomic,strong) NSMutableArray * titArray;
@property(nonatomic,assign) NSInteger page;

@property(nonatomic,strong) RegisteredModel * regModel;
@end

@implementation systemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.titArray = [[NSMutableArray alloc] init];
    [self loadData];
    self.title = @"系统通知";
    [self.view addSubview:self.table];
    
}

-( void)setTable{
    [self setExtraCellLineHidden:self.table];
    [self.table setSeparatorInset:UIEdgeInsetsZero];
    [self.view addSubview:self.table];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;   
}
- (void)setExtraCellLineHidden:(UITableView *)tab{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tab setTableFooterView:view];
}
-(UITableView *)table{
    if (!_table) {
        _table = [[UITableView alloc ] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kTabHeight) style:UITableViewStyleGrouped];
        self.table.delegate = self;
        self.table.dataSource = self;
        _table.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self loadData];
        }];
        //        _table.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        //            self.page++;
        //            [self loadData];
        //        }];
        footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreVideo)];
        [footer setTitle:@"暂无更多数据" forState:MJRefreshStateRefreshing];
        [footer setTitle:@"暂无更多数据" forState:MJRefreshStateNoMoreData];
        [self->footer setTitle:@"" forState:MJRefreshStateIdle];
        footer.stateLabel.font = [UIFont systemFontOfSize:15.0f];
        footer.automaticallyHidden = YES;
        _table.mj_footer = footer;
    }
    return _table;
}

-(void)getMoreVideo{
    self.page ++;
    [self loadData];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    
    view.backgroundColor = kColorFromRGB(249, 247, 255);
    return view;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"%ld,%ld",[indexPath section],[indexPath row]];
    messageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[messageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    RegisteredModelResult *list = self.titArray[indexPath.row];
    cell.titLab.text = list.name;
    cell.timeLab.text = list.createTime;
    [cell.headerImg sd_setImageWithURL:[NSURL URLWithString:list.companyImage] placeholderImage:[UIImage imageNamed:@"userHeaderImg"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)loadData{
    NSDictionary *dic = @{
        @"admissionStatus":@"5",
        @"pageNo":[NSString stringWithFormat:@"%ld",(long)self.page],
        @"pageSize":@"10",
    };
    __weak typeof(self) weakSelf = self;
    [[NetWorkManager sharedManager] getData:@"api/jianzhi/getSignupByUser" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            NSArray *arr = responseObject[@"result"];
            if (weakSelf.page == 1) {
                [weakSelf.titArray removeAllObjects];
            }
            weakSelf.regModel = [RegisteredModel yy_modelWithJSON:responseObject];
            if (arr.count <= 0) {
                //                [weakSelf.table.mj_footer endRefreshingWithNoMoreData];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->footer setTitle:@"暂无更多数据" forState:MJRefreshStateNoMoreData];
                    //                    if ([self.table.mj_footer isRefreshing]) {
                    //                        self.table.mj_footer.hidden = YES;
                    //                    }
                });
            }else{
                [weakSelf.titArray addObjectsFromArray:weakSelf.regModel.result];
            }
            //
            if (self.regModel.result.count > 0) {
                [weakSelf.table.mj_footer endRefreshingWithNoMoreData];
            }else{
                
            }
            if (arr == 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->footer setTitle:@"暂无更多" forState:MJRefreshStateIdle];
                });
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.table reloadData];
                [weakSelf.table.mj_footer endRefreshing];
                [weakSelf.table.mj_header endRefreshing];
            });
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [HWProgressHUD showMessage:@"请联系管理员"];
            [weakSelf.table.mj_header endRefreshing];
            [weakSelf.table.mj_footer endRefreshing];
        });
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     
        return KSCHEIGHT(75);
    
}
@end
